library(foreign)
library(mgcv)
mydata<-read.dta("C:/Users/tbohmelt/Dropbox/Democracy and Deforestation/QoG/GAM/Deforestation.dta")
attach(mydata)

model1<-gam (LDV1~s(p_polity2, k=4,fx=TRUE) + forestl + as.factor(year) + as.factor(ccode),family=gaussian)
summary(model1)
model2<-gam (LDV1~s(vdem_polyarchy, k=4,fx=TRUE) + forestl + as.factor(year) + as.factor(ccode),family=gaussian)
summary(model2)
model3<-gam (LDV1~s(fh_pr, k=4,fx=TRUE) + forestl + as.factor(year) + as.factor(ccode),family=gaussian)
summary(model3)
par(mfrow=c(3,1))
plot(model1,residuals=FALSE,se=TRUE,jit=TRUE,shade=TRUE,xlab="Democracy - Polity V",ylab="Forest Change - Sanford (2023)",ylim=c(-1.5, 1.5))
plot(model2,residuals=FALSE,se=TRUE,jit=TRUE,shade=TRUE,xlab="Democracy - V-Dem",ylab="Forest Change - Sanford (2023)",ylim=c(-1.5, 1.5))
plot(model3,residuals=FALSE,se=TRUE,jit=TRUE,shade=TRUE,xlab="Democracy - Freedom House",ylab="Forest Change - Sanford (2023)",ylim=c(-1.5, 1.5))

model4<-gam (LDV2~s(p_polity2, k=4,fx=TRUE) + lagged+ as.factor(year) + as.factor(ccode),family=gaussian)
summary(model4)
model5<-gam (LDV2~s(vdem_polyarchy, k=4,fx=TRUE) + lagged+ as.factor(year) + as.factor(ccode),family=gaussian)
summary(model5)
model6<-gam (LDV2~s(fh_pr, k=4,fx=TRUE) + lagged+ as.factor(year) + as.factor(ccode),family=gaussian)
summary(model6)
par(mfrow=c(3,1))
plot(model4,residuals=FALSE,se=TRUE,jit=TRUE,shade=TRUE,xlab="Democracy - Polity V",ylab="Forest Change - FAO Data",ylim=c(-0.1, 0.2))
plot(model5,residuals=FALSE,se=TRUE,jit=TRUE,shade=TRUE,xlab="Democracy - V-Dem",ylab="Forest Change - FAO Data",ylim=c(-0.1, 0.2))
plot(model6,residuals=FALSE,se=TRUE,jit=TRUE,shade=TRUE,xlab="Democracy - Freedom House",ylab="Forest Change - FAO Data",ylim=c(-0.1, 0.2))
